package com.baidu.customview.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.baidu.customview.R;

/**
 * Created by Kvin on 2016/1/8.
 */
public class TestImageView extends ImageView {
    private Paint paint;
    private Bitmap bitmap;
    private int imgType = 2;
    public static final int TYPE_RES = 1;
    public static final int TYPE_COMPRESS = 2;

    private Matrix mShaderMatrix = new Matrix();
    private BitmapShader mBitmapShader;
    private final RectF mDrawableRect = new RectF();

    private int mBitmapWidth;
    private int mBitmapHeight;

    public TestImageView(Context context) {
        super(context);
    }

    public TestImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
    }

    private void initAttr(Context context, AttributeSet attrs) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
    }

    public TestImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TestImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttr(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bitmap == null) {
            return;
        }
        Bitmap b=BitmapFactory.decodeResource(getResources(), R.mipmap.peien_thinking);
        canvas.drawBitmap(b, 100, 200, paint);

        paint.setColor(Color.RED);

        paint.setTextSize(30);
        canvas.drawText("I come back again",0,400,paint);
    }

    @Override
    public void setImageResource(int resId) {
        Log.e("TAG", "=======>setImageResource");
        super.setImageResource(resId);
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        Log.e("TAG", "=======>setImageBitmap");
        super.setImageBitmap(bm);
    }

    //match with src
    @Override
    public void setImageDrawable(Drawable drawable) {
        Log.e("TAG", "=======>setImageDrawable");
        super.setImageDrawable(drawable);
        bitmap = getBitmapFromDrawable(drawable);
    }

    @Override
    public void setImageURI(Uri uri) {
        Log.e("TAG", "=======>setImageURI");
        super.setImageURI(uri);
    }

    private Bitmap getBitmapFromDrawable(Drawable drawable) {
        if (drawable == null) {
            return null;
        }
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        } else {
            return null;//not handle when background is a color
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        init();
    }

    private void init() {
        setScaleType(ScaleType.CENTER);
        //setWillNotDraw(false);//call onDraw() Method
       // mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        if (imgType == TYPE_COMPRESS) {
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
            mBitmapWidth = bitmap.getWidth();
            mBitmapHeight = bitmap.getHeight();
        } else {
            return;
        }
       // mDrawableRect.inset(mBitmapWidth,mBitmapHeight);
        //updateShaderMatrix();
        invalidate();

    }

    private void updateShaderMatrix() {
        float scale;
        float dx = 0;
        float dy = 0;

        mShaderMatrix.set(null);

        if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
            scale = mDrawableRect.height() / (float) mBitmapHeight;
            dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
        } else {
            scale = mDrawableRect.width() / (float) mBitmapWidth;
            dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
        }

        mShaderMatrix.setScale(scale, scale);
        mShaderMatrix.postTranslate((int) (dx + 0.5f) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top);

        mBitmapShader.setLocalMatrix(mShaderMatrix);
    }
}
